Blog

Brad Wood

December 29, 2012

Spread the word


Share your thoughts

The request collection is a struct of values which are aggregated from the FORM and URL scopes as well as remote parameters to proxy requests.  The data in your request collection is available via methods in the event object or by referencing the "rc" struct directly.
 
event.getValue('foo')
or
rc.foo
 
What do you do when you need to deal with item in the rc (or request collection) that might not exist?  Common scenarios would be check boxes that may be left blank, or optional URL parameters.  In those instances you may want to provide a default value, or simply test for the existence of the item.
 
The first thing you might think to do would be typical manipulation of the rc struct.  Remember, structs are passed by reference, so any changes made to rc in an event handler for instance, are made for the remainder of that request.
 
// Modify the rc with a default value
param name='rc.foo' default='bar';
 
// Check for the existence of a key
structKeyExists(rc,'foo')
 
// Return a default without modifying the rc
if(structKeyExists(rc,'foo')) {
    return rc.foo;
} else {
    return 'default';
}
 
Those methods will work, but there are cleaner, more consistent ways to accomplish the same end using the request context, or event object.
 
// Modify the rc with a default value
event.paramValue('foo','bar');
 
// Check for the existence of a key
event.valueExists('foo');
 
// Return a default without modifying the rc
event.getValue('foo','default');
 
Hopefully these simple but effective helper methods in the request context will make productive additions to your toolbox.  
 
 
P.S. Other useful methods: 
event.getTrimValue('foo')
event.removeValue('foo');
event.collectionAppend({foo='bar',fu='barre'});

Add Your Comment

(6)

Mar 19, 2013 02:18:17 UTC

by Chris Galli

event.getValue('foo','default'); is not returning as I am understanding it should. I am using prc.isWidget = event.getValue('arguments.isWidget','false'); or prc.isWidget = event.getValue('arguments.isWidget',false); But both return false when my arguments.isWidget value is true by using if (arguments.isWidget){prc.isWidget = true;}else{prc.isWidget = false;}; I get the desired behavior. Am I using this incorrectly?

Mar 19, 2013 02:46:43 UTC

by Brad Wood

You're misunderstanding the event.getValue() method. It is not a general purpose method for accessing any variables in the function but instead a special method specifically for getting values from the request collection. If you do event.getValue('arguments.foo') that is looking for a key called "arguments.foo" in the request collection struct, or rc["arguments.foo"] which is obviously not what you want. If you want to deal with values coming into the arguments scope, you want to use the regular functionality of CFML to deal with that such as CFParam, structKeyExists() and the like. Only use the methods in the event object to work with the request collection.

Mar 19, 2013 11:57:01 UTC

by Chris Galli

Thanks. That makes sense now. Something more like param arguments.isWidget = event.getValue('rc.isWidget','false'); pr.isWidegt = arguments.isWidget; should give the arguments scope priority while gracefully delegating to the rc and then to a default value.

Mar 19, 2013 17:27:41 UTC

by Chris Galli

I discovered I do not need to reference the rc in the event.get value. <br><br> param arguments.isWidget = event.getValue('isWidget','false'); <br><br> prc.isWidget = arguments.isWidget;

Mar 19, 2013 18:00:52 UTC

by Brad Wood

Yep, I was going to comment to that effect but you beat me to it. You only have to pass in the exact name of the variable in the rc to the getValue method. Otherwise it would be looking for rc["rc.foo"] which wouldn't exist. Think of it this way: return event.getValue("foobar"); is the exact same as: var rc = event.getCollection(); return rc.foobar;

Mar 19, 2013 18:01:44 UTC

by Brad Wood

Wow, that's annoying that all our line breaks keep getting eaten-- I'm going to put in a ticket for that :)

Recent Entries

Ortus June 2024 Newsletter!

Ortus June 2024 Newsletter!

Welcome to the latest edition of the Ortus Newsletter! This month, we're excited to bring you highlights from our sessions at CFCamp and Open South Code, as well as a sneak peek into our upcoming events. Discover the latest developments in BoxLang, our dynamic new JVM language, and catch up on all the insightful presentations by our expert team. Let's dive in!

Maria Jose Herrera
Maria Jose Herrera
June 28, 2024
BoxLang June 2024 Newsletter!

BoxLang June 2024 Newsletter!

We're thrilled to bring you the latest updates and exciting developments from the world of BoxLang. This month, we're diving into the newest beta release, introducing a new podcast series, showcasing innovative integrations, and sharing insights from recent events. Whether you're a seasoned developer or just getting started, there's something here for everyone to explore and enjoy.

Maria Jose Herrera
Maria Jose Herrera
June 28, 2024
BoxLang 1.0.0 Beta 3 Launched

BoxLang 1.0.0 Beta 3 Launched

We are thrilled to announce the release of BoxLang 1.0.0-Beta 3! This latest beta version is packed with exciting new features and essential bug fixes, including robust encryption functionality, enhanced Java interoperability, and more efficient event handling. Key highlights include the introduction of query caching capabilities, seamless coercion of Java Single Abstract Method (SAM) interfaces from BoxLang functions, and support for virtual thread executors. So, let’s dive into the details of what’s new in BoxLang 1.0.0-Beta 3 and how you can start leveraging these updates today!

Luis Majano
Luis Majano
June 28, 2024